package com.example.neusoft.cook.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.neusoft.cook.Adapter.FoodFavorListAdapter;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.ViewHolder.FoodFavorListViewHolder;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.SampleFavor;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class FoodFavoritesFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private FoodFavorListAdapter adapter;


    public FoodFavoritesFragment() {
        // Required empty public constructor
    }

    private void getFavoritesFood(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<SampleFavor>> call = retrofitService.getAllUserCollection(getUserId(),"1");
        call.enqueue(new Callback<List<SampleFavor>>() {
            @Override
            public void onResponse(Call<List<SampleFavor>> call, Response<List<SampleFavor>> response) {
                if (getActivity() !=null){
                    adapter = new FoodFavorListAdapter(getActivity(),response.body());
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<SampleFavor>> call, Throwable t) {

            }
        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_food);
        getFavoritesFood();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_food_favorites, container, false);
    }

}
